package com.fs.sys.web;

import com.fs.common.bean.UserContextHolder;
import com.fs.sys.bean.dto.WorkbenchDTO;
import com.fs.sys.bean.query.WorkbenchQuery;
import com.fs.sys.service.WorkbenchService;
import com.fs.sys.entityservice.WorkbenchEntityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工作台控制器 
 * @author Coder Generator 2025-06-15 11:57:04 
 **/
@RestController
@RequestMapping("/sys/workbench")
@Slf4j
@Tag(name = "工作台控制器")
public class WorkbenchController {
    @Autowired
    private WorkbenchEntityService service;

    @Autowired
    private WorkbenchService workbenchService;

    @PostMapping("add")
    @Operation(summary = "新增")
    public void add(@RequestBody @Validated WorkbenchDTO req) {
        service.insert(req);
    }

    @PutMapping("update")
    @Operation(summary = "更新")
    public void update(@RequestBody @Validated WorkbenchDTO req) {
        service.update(req);
    }

    @DeleteMapping("delete/{id}")
    @Operation(summary = "删除")
    public void delete(@PathVariable("id") String id) {
        service.delete(id);
    }

    @GetMapping("detail/{id}")
    @Operation(summary = "根据id查找记录")
    public WorkbenchDTO findById(@PathVariable("id") String id) {
        return service.findById(id).orElse(null);
    }

    @PostMapping("query")
    @Operation(summary = "查询-不分页")
    public List<WorkbenchDTO> query(@RequestBody WorkbenchQuery query) {
        return service.query(query);
    }

    /**
     * 获取租户全局工作台
     */
    @GetMapping("tenant")
    public WorkbenchDTO getTenantWorkbench() {
        return workbenchService.getTenantWorkbench();
    }

    /**
     * 获取用户个性化的工作台配置
     * @return 用户个性化工作台配置，返回数据可能为空
     */
    @GetMapping("user")
    public List<WorkbenchDTO> getUserWorkbenches() {
        String userId = UserContextHolder.getUserIdOrThrow();
        return workbenchService.getUserWorkbench(userId);
    }
}